<template>
  <el-card shadow="never" class="border-0">
    <!-- 新增|刷新 -->
    <AddHeader
      @create="handleCreate"
      @refresh="getData"
      @delete="handeMultiDelete"
      layout="create,delete,refresh"
    />

    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      ref="multipleSelection"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        label="规格名称"
        width="200"
        prop="name"
      ></el-table-column>
      <el-table-column label="规格值" width="180" prop="default">
      </el-table-column>
      <el-table-column label="排序" prop="order"> </el-table-column>
      <el-table-column label="状态" width="120">
        <template #default="{ row }">
          <el-switch
            :modelValue="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="updateStatus($event, row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" fixed="right" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            text
            @click="handleEdit(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="flex items-center justify-center mt-5">
      <el-pagination
        background
        layout="prev, pager ,next"
        :total="total"
        :current-page="currentPage"
        :page-size="limit"
        @current-change="getData"
      />
    </div>

    <FormDrawer
      ref="formDrawerRef"
      :title="drawerTitle"
      @submit="handleSubmit"
      destroyOnClose
    >
      <el-form
        :model="form"
        ref="formRef"
        :rules="rules"
        label-width="80px"
        :inline="false"
      >
        <el-form-item label="name" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="order" prop="order">
          <el-input-number v-model="form.order" :min="1" :max="9999" />
        </el-form-item>
        <el-form-item label="status" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="default" prop="default">
          <TagInput v-model="form.default" />
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>
<script setup>
import { ref } from 'vue'
import FormDrawer from '@/components/FormDrawer.vue'
import AddHeader from '@/components/AddHeader.vue'
import TagInput from '@/components/TagInput.vue'
import {
  getSkus,
  addSkus,
  updateSkus,
  deleteAllSkus,
  updateSkusStatus,
} from '@/api/http.js'
import { useInitTable, useInitForm } from '@/composables/useCommon'

const {
  resetSearchForm,
  tableData,
  loading,
  currentPage,
  total,
  getData,
  limit,
  multipleSelectionRef,
  handeMultiDelete,
  handleSelectionChange,
  updateStatus,
} = useInitTable({
  getData: getSkus,
  delete: deleteAllSkus,
  updateStatus: updateSkusStatus,
})

const {
  formDrawerRef,
  formRef,
  form,
  editId,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
  handleDelete,
  rules,
} = useInitForm({
  getData,
  update: updateSkus,
  create: addSkus,
  delete: deleteAllSkus,
  form: {
    name: '',
    order: 1000,
    status: 1,
    default: '',
  },
  rules: {
    name: [
      {
        required: true,
        trigger: 'blur',
      },
    ],
    default: [
      {
        required: true,
        trigger: 'blur',
      },
    ],
  },
})
</script>
